import {
	BASE_URL
} from "@/config/app.js"
import graceJS from '@/Grace6/js/grace.js';
export const apiResquest = (prams) => { //prams 为我们需要调用的接口API的参数 下面会贴具体代码
	let dataObj = null
	let headerData = {
		'Content-Type': 'application/json; charset=UTF-8',
		'dataType': 'json',
		'Authorization': uni.getStorageSync('token')
	}
  console.log('data',prams.query );
  const data = JSON.stringify(prams.query)
	return new Promise((resolve, reject) => {
		let url = BASE_URL + prams.url; //请求的网络地址和局地的api地址组合
		return uni.request({
			url: url,
			data: data,
			method: prams.method,
			header: headerData,
			success: (res) => {
				console.log('请求返回：',res);
				if (res.data.code == 401) {
					uni.showToast({
						title:'登录过期，请重新登录',
						duration: 1000,
						icon: "error"
					})
					graceJS.removeStorage('token');
					uni.navigateTo({
						url: '/pages/login/login'
					});
				} else {
					resolve(res.data);
				}
			},
			fail: (err) => {
				uni.showToast({
					title: err.errMsg,
					duration: 1000,
					icon: "error"
				})
				reject(err);
			},
			complete: () => {

			}
		});
	})
}


// 文件上传
export const uploadResquest = (prams) => { //prams 为我们需要调用的接口API的参数 下面会贴具体代码
	let headerData = {
		'Authorization': uni.getStorageSync('token')
	}
	return new Promise((resolve, reject) => {
		let url = BASE_URL + prams.url; //请求的网络地址和局地的api地址组合
		uni.showLoading({
			title: ' 上传中',
			mask: true
		})

		uni.uploadFile({
			url: url, //仅为示例，非真实的接口地址
			filePath: prams.filePath,
			name: prams.name,
			header: headerData,
			formData: prams.formData,
			success: (res) => {
				let data = JSON.parse(res.data);
				if (data.code == '2') {
					graceJS.removeStorage('token');
					uni.navigateTo({
						url: '/pages/login/login'
					});
				} else {
					resolve(data);
				}
			},
			fail: (err) => {
				reject(err);
			},
			complete: () => {
				uni.hideLoading()
			}
		});

	})
}
